package com.huonan.component.excel;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Font;

import java.util.List;

public class ExcelComponent {

    /**
     * 每次写入Excel的条数，一个批次flush一次
     */
    public static final int BATCH_SIZE = 1000;

    /**
     * @param file     写入文件，全路径
     * @param headRow  第一行标题
     * @param dataList 数据
     */
    public <T> void writeExcel(String file, List<String> headRow, List<T> dataList) {
        List<List<T>> split = ListUtil.split(dataList, BATCH_SIZE);
        BigExcelWriter bigWriter = ExcelUtil.getBigWriter(file);
        try {
            bigWriter.writeHeadRow(headRow);
            for (List<T> list : split) {
                bigWriter.write(list, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bigWriter.flush();
            bigWriter.close();
        }
    }

    public void writeExcel(String file, List<String> headRow, List<String> dataList, String sheetName) {
        List<List<String>> split = ListUtil.split(dataList, BATCH_SIZE);
        BigExcelWriter bigWriter = ExcelUtil.getBigWriter(file);
        if (StrUtil.isNotEmpty(sheetName)) {
            bigWriter.setSheet(sheetName);
        }
        try {
            bigWriter.writeHeadRow(headRow);
            for (List<String> list : split) {
                bigWriter.write(list, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bigWriter.flush();
            bigWriter.close();
        }
    }

    /**
     * 读取Excel所有
     *
     * @param file     Excel 文件
     * @param beanType 读取要转换的实体类型
     * @return 实体数组
     */
    public List<T> readAllExcel(String file, Class<T> beanType) {
        return read(file, beanType, 0, 1);
    }

    /**
     * @param file      Excel 文件
     * @param beanType  读取要转换的实体类型
     * @param headerRow 标题所在行
     * @param startRow  读取数据开始行
     * @return @return 实体数组
     */
    public List<T> read(String file, Class<T> beanType, int headerRow, int startRow) {
        return read(file, beanType, headerRow, startRow, Integer.MAX_VALUE);
    }


    /**
     * @param file      Excel 文件
     * @param beanType  读取要转换的实体类型
     * @param headerRow 标题所在行
     * @param startRow  读取数据开始行
     * @param endRow    读取数据结束行
     * @return @return 实体数组
     */
    public <T> List<T> read(String file, Class<T> beanType, int headerRow, int startRow, int endRow) {
        try (ExcelReader reader = ExcelUtil.getReader(file);) {
            reader.read(headerRow, startRow);
            return reader.read(headerRow, startRow, endRow, beanType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ListUtil.empty();
    }

    /**
     * 设置样式
     *
     * @param writer 写入工具
     */
    public void setStyle(BigExcelWriter writer) {
        Font font = writer.createFont();
        font.setBold(true);
        font.setColor(Font.COLOR_RED);
        writer.getStyleSet().setFont(font, true);//格式可以生效，对整个单元格生效或者除head
        writer.getHeadCellStyle().setFont(font);//格式可以生效，对head生效
    }


}
